//
//  StatusRetweetedView.swift
//  上海微博
//
//  Created by mac on 16/5/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class StatusRetweetedView : UIView {
    //设置数据
    var statusModel: StatusViewModel? {
        didSet{
            
            //隔离没有转发微博
            if statusModel?.status.retweeted_status==nil {
                print("没有转发微博")
                //更新被转发微博控件
                reweedLabel.snp_updateConstraints { (make) in
                    make.top.equalTo(self)
                }
                pictureView.snp_updateConstraints { (make) in
                    make.size.equalTo(CGSizeZero)
                    make.top.equalTo(reweedLabel.snp_bottom)
                    make.bottom.equalTo(self)
                
            }
            return
        }
            //设置被转发微博文本
            reweedLabel.text=statusModel?.retweedText
            
            var offset:CGFloat = 0
            if statusModel?.status.retweeted_status?.pic_urls?.count>0 {
                offset=cz_statusLayout.margin
            }
            
            reweedLabel.snp_updateConstraints { (make) in
                make.top.equalTo(self).offset(cz_statusLayout.margin)
            }
            
            //更新配图视图大小
            pictureView.snp_updateConstraints { (make) in
                make.size.equalTo((statusModel?.RetweetedPctureSize)!)
                make.top.equalTo(reweedLabel.snp_bottom).offset(offset)
                
                make.bottom.equalTo(self).offset(-cz_statusLayout.margin)
            }
            //设置图像
            pictureView.pic_url=statusModel?.status.retweeted_status?.pic_urls
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 设置私有控件
    private let reweedLabel: UILabel = UILabel(cz_text: "@作者:微博", fontSize: 14)
    //配图视图
    private let pictureView: StatusPicture = StatusPicture()
    
}

///MARK: 设置界面
extension StatusRetweetedView {
    private func setupUI(){
        backgroundColor=UIColor.cz_colorWithHex(0xF5F5F5)
        //添加控件
        addSubview(reweedLabel)
        addSubview(pictureView)
        
        //自动布局
        let layout = cz_statusLayout
        reweedLabel.snp_makeConstraints { (make) in
            make.right.equalTo(self).offset(layout.margin)
            make.left.equalTo(self).offset(layout.margin)
            make.top.equalTo(self).offset(layout.margin)
        }
        pictureView.snp_makeConstraints { (make) in
            make.left.equalTo(reweedLabel)
            make.top.equalTo(reweedLabel.snp_bottom).offset(layout.margin)
            
            //设置大小
            make.size.equalTo(CGSizeMake(150, 150))
            make.bottom.equalTo(self).offset(-layout.margin)
        }
    }
}